from haystack import indexes
from course.models import CourseModel


class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    cname = indexes.CharField(model_attr="cname")
    content = indexes.CharField(model_attr="content")
    teacher = indexes.CharField(model_attr="teacher")
    id = indexes.IntegerField(model_attr="id")
    cover = indexes.CharField(model_attr="cover")
    def get_model(self):
        return CourseModel

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def prepare_text(self, obj):
        return f"{obj.cname}{obj.content}"
